import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isArray from "lodash/isArray";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _class, _class2;

/**
 * 数据层级配置
 */
import React, { PureComponent } from 'react';
import { Form, Menu, Spin } from 'antd';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import Lov from "../../../components/Lov";
import intl from "../../../utils/intl";
import { getCurrentOrganizationId, getResponse } from "../../../utils/utils";
import notification from "../../../utils/notification";
import { queryLov, queryLovData } from "../../../services/api";
import style from "./index.less?modules";
/**
 * 数据层级配置
 *
 * @extends {PureComponent} - React.PureComponent
 * @reactProps {!boolean} [modalVisible=false] - 是否显示选择数据层级的模态框
 * @reactProps {!array} [expandedRowKeys=[]] - 显示的数据层级值id
 * @reactProps {!array} [renderTree=[]] - 数据层级列表数据
 * @reactProps {!array} [queryParams=[]] - 值集视图配置
 * @reactProps {!string} [dataHierarchyCode] - 数据层级编码
 * @reactProps {!string} [dataHierarchyMeaning] - 数据层级值展示值
 * @returns React.element
 */

var DefaultDataHierarchies = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(DefaultDataHierarchies, _PureComponent);

  var _super = _createSuper(DefaultDataHierarchies);

  /**
   * constructor - constructor方法
   * 组件构造函数
   */
  function DefaultDataHierarchies(props) {
    var _this;

    _classCallCheck(this, DefaultDataHierarchies);

    _this = _super.call(this, props);
    _this.state = {
      dataHierarchyCode: '',
      // 配置编码
      // dataHierarchyMeaning: '', // 配置显示值
      // dataHierarchyValue: '',
      valueField: '',
      displayField: '',
      dataHierarchyName: '',
      hierarchicalList: []
    };
    return _this;
  }

  _createClass(DefaultDataHierarchies, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          _this$props$dataHiera = _this$props.dataHierarchyCode,
          dataHierarchyCode = _this$props$dataHiera === void 0 ? 'TEST.DH.TENANT' : _this$props$dataHiera;
      dispatch({
        type: 'user/fetchDataHierarchiesSelectList',
        payload: {
          organizationId: getCurrentOrganizationId(),
          dataHierarchyCode: dataHierarchyCode
        }
      }).then(function (res) {
        if (res && getResponse(res)) {
          _this2.setState({
            dataHierarchyName: res.dataHierarchyName,
            // dataHierarchyValue: res.dataHierarchyValue,
            trueDataHierarchyValue: res.dataHierarchyMeaning,
            valueSourceCode: res.valueSourceCode,
            // dataHierarchyMeaning: res.dataHierarchyMeaning,
            dataHierarchyCode: dataHierarchyCode
          });

          queryLov({
            viewCode: res.valueSourceCode
          }).then(function (resp) {
            if (getResponse(resp)) {
              var ret = resp.queryUrl;
              var organizationRe = /\{organizationId\}|\{tenantId\}/g;

              if (organizationRe.test(resp.queryUrl)) {
                ret = ret.replace(organizationRe, getCurrentOrganizationId());
              }

              var valueField = resp.valueField,
                  displayField = resp.displayField;

              _this2.setState({
                valueField: valueField,
                displayField: displayField
              });

              queryLovData(ret, {
                page: 0,
                size: 5
              }).then(function (res2) {
                if (getResponse(res2)) {
                  if (_isArray(res2)) {
                    _this2.setState({
                      hierarchicalList: res2.slice(0, 5) || []
                    });
                  } else {
                    _this2.setState({
                      hierarchicalList: res2.content || []
                    });
                  }
                }
              });
            }
          });
        }
      });
    }
    /**
     * @function handleChange - 切换层级配置
     */

  }, {
    key: "handleHierarchyChange",
    value: function handleHierarchyChange(_ref) {
      var key = _ref.key;
      var _this$state = this.state,
          valueField = _this$state.valueField,
          displayField = _this$state.displayField,
          hierarchicalList = _this$state.hierarchicalList;
      var trueDataHierarchyValue = this.props.trueDataHierarchyValue;
      var arr = hierarchicalList.filter(function (item) {
        return item[valueField].toString() === key;
      });

      if (key !== 'see-all-hierarchical') {
        var _this$handleSwitch;

        // this.setState({
        //   dataHierarchyValue: key,
        //   dataHierarchyMeaning: !isEmpty(arr) ? arr[0][displayField] : trueDataHierarchyValue,
        //   trueDataHierarchyValue: !isEmpty(arr) ? arr[0][displayField] : trueDataHierarchyValue,
        // },()=>{
        this.handleSwitch((_this$handleSwitch = {}, _defineProperty(_this$handleSwitch, valueField, key), _defineProperty(_this$handleSwitch, displayField, !_isEmpty(arr) ? arr[0][displayField] : trueDataHierarchyValue), _this$handleSwitch)); // });
      }
    }
    /**
     * @function handleSwitch - 切换层级配置请求
     */

  }, {
    key: "handleSwitch",
    value: function handleSwitch(e) {
      var _this3 = this;

      var dispatch = this.props.dispatch;
      var _this$state2 = this.state,
          valueField = _this$state2.valueField,
          displayField = _this$state2.displayField,
          dataHierarchyCode = _this$state2.dataHierarchyCode;
      dispatch({
        type: 'user/switchDataHierarchies',
        payload: {
          dataHierarchyMeaning: e[displayField],
          dataHierarchyCode: dataHierarchyCode,
          dataHierarchyValue: e && e[valueField],
          organizationId: getCurrentOrganizationId()
        }
      }).then(function (res) {
        if (res) {
          if (e && e[valueField]) {
            _this3.setState({
              // dataHierarchyValue: e[displayField],
              trueDataHierarchyValue: e[displayField] // dataHierarchyMeaning: e[displayField],

            });
          }

          notification.success();
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this4 = this;

      var fetchLoading = this.props.fetchLoading;
      var _this$state3 = this.state,
          valueSourceCode = _this$state3.valueSourceCode,
          valueField = _this$state3.valueField,
          displayField = _this$state3.displayField,
          dataHierarchyName = _this$state3.dataHierarchyName,
          trueDataHierarchyValue = _this$state3.trueDataHierarchyValue,
          hierarchicalList = _this$state3.hierarchicalList;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Menu, {
        className: "default-hierarchical-select-dropdown",
        onClick: this.handleHierarchyChange
      }, fetchLoading ? /*#__PURE__*/React.createElement(Menu.Item, {
        key: "loading-spin"
      }, /*#__PURE__*/React.createElement(Spin, null)) : /*#__PURE__*/React.createElement(Menu.SubMenu, {
        className: style['select-hierarchical-wrap'],
        title: /*#__PURE__*/React.createElement("div", {
          className: style['select-hierarchical']
        }, /*#__PURE__*/React.createElement("span", null, trueDataHierarchyValue || dataHierarchyName))
      }, hierarchicalList.map(function (n) {
        return /*#__PURE__*/React.createElement(Menu.Item, {
          key: n[valueField],
          className: style['select-hierarchical-item']
        }, /*#__PURE__*/React.createElement("div", {
          className: style['select-hierarchical-item-divider']
        }, n[displayField]));
      }), /*#__PURE__*/React.createElement(Menu.Item, {
        key: "see-all-hierarchical",
        className: style['select-hierarchical-item']
      }, /*#__PURE__*/React.createElement(Lov, {
        isButton: true,
        code: valueSourceCode,
        onOk: function onOk(e) {
          _this4.handleSwitch(e);
        },
        queryParams: {
          tenantId: getCurrentOrganizationId()
        },
        className: style.lovButton
      }, intl.get('cpaas.common.view.hierarchical.seeAll').d('查看层级值'))))));
    }
  }]);

  return DefaultDataHierarchies;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleHierarchyChange", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleHierarchyChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSwitch", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSwitch"), _class2.prototype)), _class2)) || _class);
export default connect(function (_ref2) {
  var loading = _ref2.loading;
  return {
    fetchLoading: loading.effects['user/switchDataHierarchies'] || loading.effects['user/fetchDataHierarchiesSelectList']
  };
})(DefaultDataHierarchies);